
$(function() {
	$("input[id^='code']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'customer.php?do=QuoteMy.ajax_my',
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							label: item.code + " [" + item.date + ", " + item.emp_t + "]",
							value: item.code
						}
					}));
				}
			});
		},
		
		minLength: 1
	});
	
	$('#date').datepicker(_now_option);
	
	var validator = $("#form_add").validate({
		rules: {
			code: {
				required: true,
				remote: "customer.php?do=Quote.ajax_exist"
			}
		},
		messages: {
			code: {
				remote: jQuery.format("{0} 已经存在")
			},
			status: {
				required: "请选择一个状态"
			}
		},
		errorPlacement: function(label, element) {
			label.insertAfter(element);
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});
	
	_currency($('#currency').val());
	
	$('#currency').change(function() {
		var s = $(this).val();
		_currency(s);
	});
	
	$('#amount').keyup(function() {
		_update();
	});

	$('textarea').css("width", 400);
});

function total() {
	var total = 0;
	var amount = 0;
	var rmb = 0;
	$.each(data, function(k, v) {
		if (v.ok) {
			total = fxadd(total, v.amount);
		}		
	});
		
	$("#total_t").html(total);
}

function _currency(s) {
	if (s == null || s == '-')　{		
		return false;
	}
	
	var body = { };
	body.currency = s;
	$.post('finance.php?do=Finance.ajax_exchange', body, function(s) {
		exchange = s;
		$('#rate').val(s);
		
		_update();
	});
}

function _update() {
	var a = $("#amount").val();
	var b = $("#currency").find("option:selected").text();

	Currency.upper(a, b, "#label_amount");
	
	_rmb();
} 

function _rmb() {
	var a = $("#amount").val();
	var b = $('#rate').val();
	var rmb = fxmul(a, b);
	
	$('#rmb').val(rmb);	
}

